package com.user.gamecenter.manager;

import android.content.Intent;
import android.util.Log;

import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadListener;
import com.liulishuo.filedownloader.FileDownloader;
import com.user.gamecenter.app.MyApp;

import org.apache.commons.io.FileUtils;

import java.io.File;

/**
 * author : Administrator
 * e-mail : 254419013@qq.com
 * time   : 2017/06/29
 * desc   :
 */
public class UpdateManager {


    public int downloadId = 0;

    private static class LazyHolder {
        private static final UpdateManager INSTANCE = new UpdateManager();
    }

    private UpdateManager() {

    }

    public static UpdateManager instance() {
        return LazyHolder.INSTANCE;
    }


    public synchronized void downloadFile(String url) {
        if (downloadId == 0) {
            FileUtils.deleteQuietly(new File(MyApp.plugInPath));
            FileDownloader.getImpl().clearAllTaskData();
            downloadId = FileDownloader.getImpl().create(url)
                    .setPath(MyApp.plugInPath)
                    .setForceReDownload(true)//强制下载
                    .setListener(fileDownloadListener)
                    .start();
            Log.d("UpdateManager", "1----------------- downloadId:" + downloadId);
        }

    }

    //普通下载
    private FileDownloadListener fileDownloadListener = new FileDownloadListener() {
        @Override
        protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
            Log.d("UpdateActivity", "1------------- pending");
        }

        @Override
        protected void connected(BaseDownloadTask task, String etag, boolean isContinue, int soFarBytes, int totalBytes) {
            Log.d("UpdateActivity", "1------------- connected");
        }

        @Override
        protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {

            int progress = (int) (((double) soFarBytes / (double) totalBytes) * 100.0);
            Log.d("UpdateActivity", "1------------- progress:" + progress);

        }

        @Override
        protected void blockComplete(BaseDownloadTask task) {
            Log.d("UpdateActivity", "1------------- blockComplete");

        }

        @Override
        protected void retry(final BaseDownloadTask task, final Throwable ex, final int retryingTimes, final int soFarBytes) {
            Log.d("UpdateActivity", "1------------- retry");
        }

        @Override
        protected void completed(BaseDownloadTask task) {
            Log.d("UpdateActivity", "1------------ completed " + task.getFilename());
            File fileDirectory = new File(MyApp.plugInApkDirectoryPath);
            File resFile = fileDirectory.listFiles()[0];

            Log.d("UpdateManager", "1------------------" + resFile.getAbsolutePath());
            if (!resFile.getAbsolutePath().equals(MyApp.plugInPath)) {
                Log.d("UpdateManager", "1------------------ File name is not the same");
                File newFile = new File(MyApp.plugInPath);
                resFile.renameTo(newFile);
            } else {
                Log.d("UpdateManager", "1------------------ file name is the same");
            }
            PlugInManager.removePlugIn();
            PlugInManager.install(MyApp.plugInPath);
            Intent launchIntent = MyApp.context.getPackageManager().getLaunchIntentForPackage(MyApp.plugInPackageName);
            MyApp.context.startActivity(launchIntent);
            downloadId = 0;
        }

        @Override
        protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
            Log.d("UpdateActivity", "1------------- paused");
        }

        @Override
        protected void error(BaseDownloadTask task, Throwable e) {
            Log.d("UpdateActivity", "1------------- error");
        }

        @Override
        protected void warn(BaseDownloadTask task) {
            Log.d("UpdateActivity", "1------------- warn");
        }
    };

}
